#include "InputComponent.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Scene/Scene.h"
#include "Widgets/ViewportWidget.h"
#include "SceneSubsystems/InputComponentManager.h"

void RInputComponent::OnBeginPlay()
{
    Super::OnBeginPlay();
    GetScene()->GetSubsystem<RInputComponentManager>()->RegisterComponent(this);
}

void RInputComponent::OnEndPlay()
{
    Super::OnEndPlay();
    GetScene()->GetSubsystem<RInputComponentManager>()->UnregisterComponent(this);
}

bool RInputComponent::OnViewportMouseButtonPressed(const SMouseButtonEvent& InMouseEvent)
{
    switch(InMouseEvent.GetButton())
    {
        case EMouseButton::Left:
        {
            OnLeftMouseButtonPressed.ExecuteIfBound();
        }
        break;

        case EMouseButton::Right:
        {
            OnRightMouseButtonPressed.ExecuteIfBound();
        }
        break;

        case EMouseButton::Middle:
        {
            OnWheelPressed.ExecuteIfBound();
        }
        break;

        default:
            assert(false && "Unaddressed situation");
    }


    return true;
}

bool RInputComponent::OnViewportMouseButtonReleased(const SMouseButtonEvent& InMouseEvent)
{
    switch(InMouseEvent.GetButton())
    {
        case EMouseButton::Left:
        {
            OnLeftMouseButtonReleased.ExecuteIfBound();
        }
        break;

        case EMouseButton::Right:
        {
            OnRightMouseButtonReleased.ExecuteIfBound();
        }
        break;

        case EMouseButton::Middle:
        {
            OnWheelReleased.ExecuteIfBound();
        }
        break;

        default:
            assert(false && "Unaddressed situation");
    }


    return true;
}

bool RInputComponent::OnViewportMouseMoved(const SMouseButtonEvent& InMouseEvent)
{
    if(!OnMouseMoved.IsBound())
    {
        return true;
    }

    OnMouseMoved.Execute(InMouseEvent.GetMoveDistance());

    return true;
}


bool RInputComponent::ProcessKeyboardEvent(const SKeyboardEvent& BoardkeyEvent)
{
    switch ( BoardkeyEvent.KeyState)
    {
        case EKeyState::Released :
        {
            if(OnKeyReleasedEvents.Contains(BoardkeyEvent.Key))
            {
                OnKeyReleasedEvents[BoardkeyEvent.Key].ExecuteIfBound();
            }

            OnKeyReleased(BoardkeyEvent.Key);
        }
        break;

        case EKeyState::Pressed:
        {
            if(OnKeyPressedEvents.Contains(BoardkeyEvent.Key))
            {
                OnKeyPressedEvents[BoardkeyEvent.Key].ExecuteIfBound();
            }

            OnKeyPressed(BoardkeyEvent.Key);
        }
        break;

        default:
            assert(false&& "Unaddressed situation");
    }
   
    return true;
}
